/*
 * @Author: xiehangyun 
 * @Date: 2025-05-16 10:34:07 
 * @Last Modified by: 肖思汗
 * @Last Modified time: 2025-07-02 13:37:29
 */

import { makeAutoObservable } from "mobx";
import myWalletApi from "./myWalletApi";
import { WithdrawalData } from "types/type";
class MyWalletStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });
    }

    money = 0; // 当前余额
    getMoney() {
        myWalletApi.getMoney().then((res) => {
            this.money = res.data;
        });
    }

    // 提现记录
    pageNum = 1;
    pageSize = 10;
    year = new Date().getFullYear();
    month = new Date().getMonth() + 1;
    withdrawalList: Array<WithdrawalData> = [];
    hasMore = true; // 是否有下一页
    loading = false; // 是否正在加载中

    setDate(year: number, month: number) {
        this.year = year;
        this.month = month;
        this.getNextWithdrawal(true);
    }

    getNextWithdrawal(reset = false) {
        if (reset) {
            this.pageNum = 1;

            this.hasMore = true;
        } else if (!this.hasMore || this.loading) {
            return;
        } else {
            this.pageNum++;
        }
        this.loading = true;

        myWalletApi.getMoneyDetail({
            pageNum: this.pageNum,
            pageSize: this.pageSize,
            year: this.year,
            month: this.month
        }).then(async res => {
            if (reset) {
                this.withdrawalList = [];
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                await new Promise(resolve => requestAnimationFrame(resolve));
                this.withdrawalList = [...res.rows];
            } else {
                this.withdrawalList = [...this.withdrawalList, ...res.rows];
            }
            this.hasMore = (res.rows.length === this.pageSize);
            this.loading = false;
        });
    }




}

const myWalletStore = new MyWalletStore();

export default myWalletStore;